package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import iks.game.*;
	import iks.utils.ui.*;
	
	public final class Hinge extends Sprite 
	{
		
		public function Hinge() 
		{
			KGame.getInstance().init(this.stage, initFunction, logicFunction, 30);
			KGame.getInstance().start();
		}
		
		private function initFunction() : void
		{
			m_points = new Vector.<VerletPoint>();
			m_sticks = new Vector.<VerletStick>();
			m_stageRect = new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight);

			// base
			var pointA : VerletPoint = makePoint(this.stage.stageWidth / 2, this.stage.stageHeight - 500);
			var pointB : VerletPoint = makePoint(0, this.stage.stageHeight);
			var pointC : VerletPoint = makePoint(this.stage.stageWidth, this.stage.stageHeight);
			
			// arm 
			var pointD : VerletPoint = makePoint(this.stage.stageWidth / 2 + 350, this.stage.stageHeight - 500);
			
			// weight
			var pointE : VerletPoint = makePoint(this.stage.stageWidth / 2 + 360, this.stage.stageHeight - 510);
			var pointF : VerletPoint = makePoint(this.stage.stageWidth / 2 + 360, this.stage.stageHeight - 490);
			var pointG : VerletPoint = makePoint(this.stage.stageWidth / 2 + 370, this.stage.stageHeight - 500);
			
			// base
			makeStick(pointA, pointB);
			makeStick(pointB, pointC);
			makeStick(pointC, pointA);
			
			// arm
			makeStick(pointA, pointD);
			
			// weight
			makeStick(pointD, pointE);
			makeStick(pointD, pointF);
			makeStick(pointE, pointF);
			makeStick(pointE, pointG);
			makeStick(pointF, pointG);
		}
		
		private function logicFunction() : void
		{
			updatePoints();
			
			for (var i : int = 0; i < 5; ++i)
			{
				constrainPoints();
				updateSticks();
			}
			
			this.graphics.clear();
			this.graphics.beginFill(0xA0A0A0);
			this.graphics.drawRect(m_stageRect.x, m_stageRect.y, m_stageRect.width, m_stageRect.height);
			renderPoints();
			renderSticks();
		}
		
		private function makePoint(xpos : Number, ypos : Number) : VerletPoint
		{
			var point : VerletPoint = new VerletPoint(xpos, ypos);
			m_points.push(point);
			return point;
		}
		
		private function makeStick(pointA : VerletPoint, pointB : VerletPoint, length : Number = -1) : VerletStick
		{
			var stick : VerletStick = new VerletStick(pointA, pointB, length);
			m_sticks.push(stick);
			return stick;
		}
		
		private function updatePoints() : void
		{
			for each (var p : VerletPoint in m_points)
			{
				p.y += 0.5;
				p.update();
			}
		}
		
		private function constrainPoints() : void
		{
			for each (var p : VerletPoint in m_points)
				p.constrain(m_stageRect);
		}
		
		private function updateSticks() : void
		{
			for each (var s : VerletStick in m_sticks)
				s.update();
		}
		
		private function renderPoints() : void
		{
			for each (var p : VerletPoint in m_points)
				p.render(this.graphics);
		}
		
		private function renderSticks() : void
		{
			for each (var s : VerletStick in m_sticks)
				s.render(this.graphics);
		}
		
		private var m_points : Vector.<VerletPoint> = null;
		private var m_sticks : Vector.<VerletStick> = null;
		private var m_stageRect : Rectangle = null;
	}

}